#!/usr/bin/python
# plot_velocities.py

import numpy
from matplotlib import use as useBackend
useBackend('Agg')
import matplotlib.pyplot as ppt

import GM_InterpRoutines as IR

# True/False switch for plotting apparent or actual velocities.
# Quantity plotted not affected; only naming.
# Use 'True' to appropriately name plots for apparent velocities.
appness = True

def cheat111():
    '''Cheat to clear plot.'''
    ppt.subplot(311)
    ppt.plot([0,1],[1,2])
    ppt.subplot(131)
    ppt.plot([0,1],[1,2])
    ppt.subplot(111)


def plot(simpars, phypars, vtuple):
    '''Shows a slice of GM_velocities's results.'''
    for i in simpars:
        cmd = "%s = simpars['%s']" % (i,i)
        exec cmd
    for i in phypars:
        cmd = "%s = phypars['%s']" % (i,i)
        exec cmd
    (vrelappl, sigmaappl2, sigmaappb2) = vtuple

    dmax = int((scale + R0)/1000. + 0.001)
    bstring = '0 -> b -> $\pi$/2'
    lstring = '0 -> l -> $\pi$'
    dstring = str(dmax)+' kpc <- d <- 0 kpc'
    lims_ld = [0, 180, dmax, 0]
    lims_bd = [0, 90, dmax, 0]
    asp_ld = 0.6*180./dmax
    asp_bd = 0.6*90./dmax

    # apparent (=angular) sigma squared l velocity
    cheat111()
    ppt.imshow(sigmaappl2[:,:,0], extent=lims_ld, aspect=asp_ld)
    ppt.colorbar()
    ppt.xlabel(lstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Sigma squared angular velocity in l dirn for b ~ 0"
        plotname = 'SigAppl2ldPlane.pdf'
    elif appness == False:
        titlename = "Sigma squared velocity in l dirn for b ~ 0"
        plotname = 'Sigl2_ldPlane.pdf'
    else:
        print "Prepare for breakage!  The variable 'appness' in plot_velocities must be boolean, fool!"
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(sigmaappl2[:,0,:], extent=lims_bd, aspect=asp_bd)
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Sigma squared angular velocity in l dirn for l ~ 0"
        plotname = 'SigAppl2bdPlaneWindward.pdf'
    if appness == False:
        titlename = "Sigma squared velocity in l dirn for l ~ 0"
        plotname = 'Sigl2_bdPlaneWindward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(sigmaappl2[:,-1,:], extent=lims_bd, aspect=asp_bd)
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Sigma squared angular velocity in l dirn for l ~ pi"
        plotname = 'SigAppl2bdPlaneLeeward.pdf'
    if appness == False:
        titlename = "Sigma squared velocity in l dirn for l ~ pi"
        plotname = 'Sigl2_bdPlaneLeeward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    # apparent (=angular) sigma squared b velocity
    cheat111()
    ppt.imshow(sigmaappb2[:,:,0], extent=lims_ld, aspect=asp_ld)
    ppt.colorbar()
    ppt.xlabel(lstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Sigma squared angular velocity in b dirn for b ~ 0"
        plotname = 'SigAppb2ldPlane.pdf'
    if appness == False:
        titlename = "Sigma squared velocity in b dirn for b ~ 0"
        plotname = 'Sigb2_ldPlane.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(sigmaappb2[:,0,:], extent=lims_bd, aspect=asp_bd)
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Sigma squared angular velocity in b dirn for l ~ 0"
        plotname = 'SigAppb2bdPlaneWindward.pdf'
    if appness == False:
        titlename = "Sigma squared velocity in b dirn for l ~ 0"
        plotname = 'Sigb2_bdPlaneWindward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(sigmaappb2[:,-1,:], extent=lims_bd, aspect=asp_bd)
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Sigma squared angular velocity in b dirn for l ~ pi"
        plotname = 'SigAppb2bdPlaneLeeward.pdf'
    if appness == False:
        titlename = "Sigma squared velocity in b dirn for l ~ pi"
        plotname = 'Sigb2_bdPlaneLeeward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    # apparent (=angular, relative) bulk l velocity
    cheat111()
    ppt.imshow(vrelappl[:,:,0])
    ppt.colorbar()
    ppt.xlabel(lstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Bulk relative angular velocity in l dirn for b ~ 0"
        plotname = 'VRelApplldPlane.pdf'
    if appness == False:
        titlename = "Bulk relative velocity in l dirn for b ~ 0"
        plotname = 'VRell_ldPlane.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(vrelappl[:,0,:])
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Bulk relative angular velocity in l dirn for l ~ 0"
        plotname = 'VRelApplbdPlaneWindward.pdf'
    if appness == False:
        titlename = "Bulk relative velocity in l dirn for l ~ 0"
        plotname = 'VRell_bdPlaneWindward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(vrelappl[:,-1,:])
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Bulk relative angular velocity in l dirn for l ~ pi"
        plotname = 'VRelApplbdPlaneLeeward.pdf'
    if appness == False:
        titlename = "Bulk relative velocity in l dirn for l ~ pi"
        plotname = 'VRell_bdPlaneLeeward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    # Representative RMS velocities for lensing events at each location
    d = dlocs*PtScaled
    dhalf = d/2.
    vrelapplhalf = IR.OldToNewLinear(d, vrelappl, dhalf)
    sigmaappb2half = IR.OldToNewLinear(d, sigmaappb2, dhalf)
    sigmaappl2half = IR.OldToNewLinear(d, sigmaappl2, dhalf)
    norotRMS = (sigmaappl2**2 + sigmaappl2half**2 + sigmaappb2**2 + sigmaappb2half**2)**0.5
    rotRMS = (norotRMS**2 + (vrelappl - vrelapplhalf)**2)**0.5

    # Velocities with rotation
    cheat111()
    ppt.imshow(rotRMS[:,:,0])
    ppt.colorbar()
    ppt.xlabel(lstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Typical RMS angular velocity in l dirn for b ~ 0"
        plotname = 'VRelAppRMSldPlane.pdf'
    if appness == False:
        titlename = "Typical RMS velocity in l dirn for b ~ 0"
        plotname = 'VRelRMSldPlane.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(rotRMS[:,0,:])
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Typical RMS angular velocity in l dirn for l ~ 0"
        plotname = 'VRelAppRMSbdPlaneLeeward.pdf'
    if appness == False:
        titlename = "Typical RMS velocity in l dirn for l ~ 0"
        plotname = 'VRelRMSbdPlaneLeeward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(rotRMS[:,-1,:])
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = "Typical RMS angular velocity in l dirn for l ~ pi"
        plotname = 'VRelAppRMSbdPlaneWindward.pdf'
    if appness == False:
        titlename = "Typical RMS velocity in l dirn for l ~ pi"
        plotname = 'VRelRMSbdPlaneWindward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    # Velocities without rotation
    cheat111()
    ppt.imshow(norotRMS[:,:,0])
    ppt.colorbar()
    ppt.xlabel(lstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = '''Typical RMS angular velocity, without rotation,
in l dirn for b ~ 0'''
        plotname = 'VRelAppNoRotRMSldPlane.pdf'
    if appness == False:
        titlename = '''Typical RMS velocity, without rotation,
in l dirn for b ~ 0'''
        plotname = 'VRelNoRotRMSldPlane.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(norotRMS[:,0,:])
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = '''Typical RMS angular velocity, without rotation,
in l dirn for l ~ 0'''
        plotname = 'VRelAppNoRotRMSbdPlaneLeeward.pdf'
    if appness == False:
        titlename = '''Typical RMS velocity, without rotation,
in l dirn for l ~ 0'''
        plotname = 'VRelNoRotRMSbdPlaneLeeward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(norotRMS[:,-1,:])
    ppt.colorbar()
    ppt.xlabel(bstring)
    ppt.ylabel(dstring)
    if appness == True:
        titlename = '''Typical RMS angular velocity, without rotation,
in l dirn for l ~ pi'''
        plotname = 'VRelAppNoRotRMSbdPlaneWindward.pdf'
    if appness == False:
        titlename = '''Typical RMS velocity, without rotation,
in l dirn for l ~ pi'''
        plotname = 'VRelNoRotRMSbdPlaneWindward.pdf'
    ppt.title(titlename)
    plotname = outdir+plotname
    ppt.savefig(plotname)

   
